//封装html常用功能
/*
    由render/index/index.html 导入common.js
    并挂载到document下，

    功能页面引用一下既可使用
    window.document.xx = window.top.docuement.xx;
*/


//系统桌面右下角通知
//title 标题
//body 内容
//icon 默认滑稽
//callback 关闭后回调
document.notification = (title, body, icon, callback) => {
    let option = {
        title,
        body,
        icon: icon
    }

    let myNotification = new window.Notification(option.title, option)
    myNotification.onclick = callback
}

//系统级对话框
//@TODO
document.openMessageBox = (title, body, type, callback) => {

}


document.print = (text) => {
    let textarea = document.querySelector("#dnf-console");
    textarea.value += text;

    //设置滚动条到最底部
    setTimeout(()=>{
        if (textarea.scrollHeight > textarea.clientHeight) {
            textarea.scrollTop = textarea.scrollHeight;
            textarea.focus();
        }
    },1);
}

document.println = (text) => {
    let textarea = document.querySelector("#dnf-console");
    textarea.value += text + "\n";

    //设置滚动条到最底部
    setTimeout(()=>{
        if (textarea.scrollHeight > textarea.clientHeight) {
            textarea.scrollTop = textarea.scrollHeight;
            textarea.focus();
        }
    },1);
}

document.printclear = () => {
    let textarea = document.querySelector("#dnf-console");
    textarea.value = ""
}





//获取uuid
GetUUID = () => {
    return 'Dxxxxxxxxxxxxxxyxxxxxxxxxxxxxxp'.replace(/[xy]/g, function (c) {
        var r = (Math.random() * 16) | 0,
            v = c == 'x' ? r : (r & 0x3) | 0x8;
        return v.toString(16);
    });
}

//代替es6的 parent.childNodes
function GetDocuemtnChildNodes(element) {
    if (!element) {
        return [];
    }

    return [].slice.call(element.childNodes).filter(e => e.nodeType == 1);
}


//分页工具
//自动创建div 手动挂载到某个元素下
//可能会iframe引用,所以不要用jq和document
class Page {

    constructor() {
        this.id = "dnfapp-page";
        this.currentPage = 0;   //当前页
        this.pageSize = 0;      //最大页
        this.count = 0;         //记录数量
        this.size = 20;         //分页大小
        this.value = null;      //关键字
        this.params = {};       //可自由存取其他信息
        this.callback = null;   //回调函数  function(page,value,limit, offset)

        this._uuid = "d" + GetUUID().replaceAll("-", ""); //生成随机字符串 用于区分不同的page实例
        this._mounted = false;  //是否挂载到页面
        this._div = null;       //挂载到页面的元素
    }

    //挂载到某个元素下
    //@param element:DocumentElement 页面元素
    //@param id:string 自定义元素id
    //@param className:string  自定义元素class
    mount(element, id, className) {
        if (!element) {
            return;
        }

        console.log("Page.mount", element);

        let div = document.createElement("span");
        if (id) {
            this.id = id;
            div.id = "dnfapp-page"
        }

        div.className = "dnfapp-page " + (className || "")
        div.style.userSelect = "none";

        element.appendChild(div);
        this._mounted = true;
        this._div = div;

        let pageInstance = this;

        //释放js事件到window
        //注意释放到最顶层window
        console.log("释放js事件", this._uuid);
        window.top[`${this._uuid}_prev`] = function (element) {
            if (pageInstance.currentPage <= 0) {
                return;
            }

            pageInstance.currentPage--;
            let { limit, offset } = pageInstance.getOffsetByPage(pageInstance.currentPage);
            pageInstance.callback(pageInstance, pageInstance.value, limit, offset);
        }

        window.top[`${this._uuid}_next`] = function (element) {
            if (pageInstance.currentPage >= pageInstance.pageSize) {
                return
            }

            pageInstance.currentPage++;
            let { limit, offset } = pageInstance.getOffsetByPage(pageInstance.currentPage);
            pageInstance.callback(pageInstance, pageInstance.value, limit, offset);
        }

        window.top[`${this._uuid}_jump`] = function (element, num) {
            if (num <= 0 || num > pageInstance.pageSize) {
                return;
            }

            if (num == pageInstance.currentPage) {
                return;
            }

            pageInstance.currentPage = num;
            let { limit, offset } = pageInstance.getOffsetByPage(pageInstance.currentPage);
            pageInstance.callback(pageInstance, pageInstance.value, limit, offset);
        }
    }

    reset() {
        this.currentPage = 1;
        this.pageSize = 0;
        this.count = 0;
        this.value = null;

        //设置页面分页
        if (this._mounted && this._div) {
            this._div.innerHTML = "";
        }
    }

    setCount(count) {
        this.count = count;
        this.pageSize = Math.ceil(count / this.size);

        //设置页面分页
        if (this._mounted && this._div) {

            //只有一页时不显示
            if (this.pageSize <= 1) {
                this._div.innerHTML = "";
                return;
            }

            let html = `<span style="color:#0044BB;cursor:pointer" onclick="window.top.${this._uuid}_prev(this)">上一页</span>`
            if (this.pageSize <= 10) {
                //10页以内直接显示数字
                for (let i = 1; i <= this.pageSize; i++) {
                    html += `<span style="color:#0044BB;cursor:pointer" onclick="window.top.${this._uuid}_jump(this,${i})">&nbsp;${i}&nbsp;</span>`
                }

            } else {
                //大于10页 显示1-6 。。。 最后一页
                for (let i = 1; i <= 6; i++) {
                    html += `<span style="color:#0044BB;cursor:pointer" onclick="window.top.${this._uuid}_jump(this,${i})">&nbsp;${i}&nbsp;</span>`
                }

                html += `<span style="color:#0044BB;cursor:pointer" ">&nbsp;...&nbsp;</span>`
                html += `<span style="color:#0044BB;cursor:pointer" onclick="window.top.${this._uuid}_jump(this,${this.pageSize})">&nbsp;${this.pageSize}&nbsp;</span>`

            }
            html += `<span style="color:#0044BB;cursor:pointer" onclick="window.top.${this._uuid}_next(this)">下一页</span>`

            this._div.innerHTML = html;
        }
    }

    //调用查询方法
    //提前设置callback、currentPage、count、value
    query() {
        let { limit, offset } = this.getOffsetByPage(this.currentPage);
        this.callback(this, this.value, limit, offset);
    }

    //获取第n页 offset和limit
    getOffsetByPage(n) {
        return {
            offset: (n - 1) * this.size,
            limit: this.size,
        };
    }

}

document.Page = Page;
